package lock.reentrantLock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 描述：演示ReentrantLock的基本用法:中断响应
 */

public class LockDemo {
    static class Outputer {
        Lock lock = new ReentrantLock();
        public void output(String name) {
            System.out.println(lock);
            int len = name.length();
            lock.lock();
            try {
                for (int i = 0; i < len; i++) {
                    System.out.print(name.charAt(i));
                }
                System.out.println();
            } finally {
                lock.unlock();
            }
        }
    }
    private static Outputer outputer = new Outputer();
    public static void main(String[] args) {
        new Thread(() -> {
            outputer.output("zhangsan");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        new Thread(() -> {
            outputer.output("lisi");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }
}
